<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Redirect;


class TestController extends Controller
{

    public function add(){
        $date_now=date("Y-m-d h:i:s");
        $now =date("h:i:s");
        echo time();die;

        $live_list = DB::table('live_show')->orderBy('date,time')->groupBy('vsteam')->having('date', '>', $date_now)->get();
        die;
        $user = DB::select("select * from live_show where date > '$date_now' group by vsteam order by `date`,`time`");

        $user = DB::select("select * from live_show where date > :data_now group by vsteam order by `date`,`time`", [':data_now'=>$date_now]);
//我们还 可以看到在执行查询的时候使用了参数绑定，以避免SQL注入。除此之//外还可以使用命名绑定：
//        $user = DB::select('select * from users where id = :id', [':id'=>1]);
        dd($user);die;

        $live_list = DB::table('live_show')
            ->where('date','>',$date_now)
            ->groupBy('vsteam')
            ->orderBy('date,time')
            ->get();

        $date_now=date("Y-m-d");
//        echo $date_now;die;
        DB::connection()->enableQueryLog();
        $sql = "select * from live_show where date > '$date_now' group by vsteam order by date,time";
        $queries = DB::getQueryLog();
        $a = end($sql);
        print_r($a);

//        return view('test.add',['list'=>$show]);
    }

    public function index(){
        $date_now=date("Y-m-d");
        $live_list = DB::table('live_show')->orderBy('date,time')->groupBy('vsteam')->having('date', '>', $date_now)->get();
        dd($live_list);die;
        return view('test.index');
    }

    public function test_do(){
        $arr = Input::all();
//        print_r($arr);die;
        $name = $arr['username'];
        $pwd = $arr['pwd'];
        $res = DB::table("username")
            ->insert([
                'username'=>$name,
                'password'=>$pwd
            ]);
//        print_r($res);die;
        if($res){
            echo "添加成功";
            return Redirect()->action('TestController@show');
        }else{
            echo "添加失败";
        }
    }

    public function show(){
        $list = DB::table('username')->paginate(2);
//        print_r($list);exit;
        return view('test.show',['list'=>$list]);
    }

    //ajax删除
    public function del(Request $request){
        $id = $request->input("order_id");
        $res = DB::table("username")
            ->where('id',$id)
            ->delete();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    //ajax添加
    public function doinsert(Request $request){
        $name = $request->input("name");
        $pwd = $request->input("pwd");
        $res = DB::table("username")
            ->insert([
                'username'=>$name,
                'password'=>$pwd
            ]);
//        print_r($res);die;
        if($res){
            echo "1";
        }else{
            echo "0";
        }
    }

    //修改哪条数据 渲染给页面
    public function update(Request $request){
        $id = $request->input('id');
        $list = DB::table('username')->where("id",$id)->first();
//        print_r($list);exit;
        return view('test.update',['list'=>$list]);
    }

    //修改
    public function doupdate(Request $request){
        $id = $request->input('id');
        $arr = $request->except("_token");
        $res = DB::table('username')->where("id",$id)->update($arr);
        if($res){
            echo "修改成功";
            return Redirect()->action('TestController@show');
        }else{
            echo "修改失败";
        }
    }



}
